package com.itheima.jobs;

import com.itheima.constant.RedisConstant;
import com.itheima.utils.QiniuUtils;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import redis.clients.jedis.JedisPool;

import java.util.Set;

public class ClearImgJob {
    /**
     * 自定义job清理垃圾图片
     */
    @Autowired
    private JedisPool jedisPool;
    private int i = 0;

    public void clearImg() {
        i = i++;
        System.out.println("num==========start"+i);
        //根据redis中保存的2个set集合进行差值计算，获得垃圾图片的名称集合
        Set<String> set = jedisPool.getResource().sdiff(RedisConstant.SETMEAL_PIC_RESOURCES, RedisConstant.SETMEAL_PIC_DB_RESOURCES);
        if (set != null && set.size() > 0) {
            for (String picName : set) {
                //删除七牛云服务器的图片
                QiniuUtils.deleteFileFromQiniu(picName);
                jedisPool.getResource().srem(RedisConstant.SETMEAL_PIC_RESOURCES, picName);
                System.out.println("delete=============="+picName);
            }
        }
        System.out.println("num==========end"+i);
    }
}
